package com.yanqu.road.server.manager.rank;

import com.yanqu.road.entity.config.beauty.BeautyInfo;
import com.yanqu.road.entity.config.patrons.PatronsInfo;
import com.yanqu.road.entity.config.skin.SkinInfo;
import com.yanqu.road.entity.config.vehicle.VehicleInfo;
import com.yanqu.road.entity.curio.config.CurioItemInfo;
import com.yanqu.road.entity.decoration.MammonRecord;
import com.yanqu.road.entity.enums.eBigRankType;
import com.yanqu.road.entity.enums.eRankType;
import com.yanqu.road.entity.enums.eServerGroup2Type;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.player.UserDecoration;
import com.yanqu.road.entity.rank.UnionRank;
import com.yanqu.road.entity.rank.cross.CrossUserDecoration;
import com.yanqu.road.entity.rank.cross.CrossUserRank;
import com.yanqu.road.entity.servercenter.ServerGroup;
import com.yanqu.road.entity.servercenter.ServerGroup2;
import com.yanqu.road.logic.bussiness.cross.CrossAreaMammonBussiness;
import com.yanqu.road.logic.bussiness.cross.CrossMammonBussiness;
import com.yanqu.road.logic.bussiness.rank.RankBussiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.cross.*;
import com.yanqu.road.pb.common.CommonProto;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.config.*;
import com.yanqu.road.server.manager.user.CrossUserNotifyMgr;
import com.yanqu.road.server.manger.IUserBaseInfoSyncListener;
import com.yanqu.road.server.manger.ServerGroup2Mgr;
import com.yanqu.road.server.manger.ServerGroupMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.ConfigHelper;
import com.yanqu.road.utils.RandomHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class CrossRankMgr extends TempMgr implements IRankDataMgr{

    private static Logger logger = LogManager.getLogger(CrossRankMgr.class.getName());

    // serverId bigType type
    private static Map<Long, Map<Integer, Map<Integer, CrossRankListModel>>> allCrossRankListModelMap;
    // groupId bigType type
    private static Map<Integer, Map<Integer, Map<Integer, CrossGroupRankListModel>>> allCrossGroupRankListModelMap;

    private static Map<Long, CrossUnionRankListModel> allCrossUnionRankListModelMap;

    private static Map<Integer, CrossGroupUnionRankListModel> allCrossGroupUnionRankListModelMap;

    // 跨服财神
    private static Map<Long, Map<Long, Map<Integer, CrossUserDecoration>>> allCrossUserMammonMap;
    private static Map<Integer, List<CrossUserDecoration>> crossGroupMammonMap;
    private static Map<Integer, Map<Integer, CrossGroupMammonListModel>> allCrossGroupMammonListModelMap;
    private static Map<Long, Map<String, MammonRecord>> allCrossMammonRecordMap;
    private static Map<Integer, Map<String, MammonRecord>> crossGroupMammonRecordMap;
    private static Map<Integer, Map<Integer, CrossGroupMammonRecordListModel>> allCrossGroupMammonRecordListModelMap;
    private static List<Long> uploadOverServerIds = new ArrayList<>();

    // 跨服的跨地区财神
    private static Map<Long, Map<Long, Map<Integer, CrossUserDecoration>>> allCrossAreaUserMammonMap;
    private static Map<Integer, List<CrossUserDecoration>> crossAreaGroupMammonMap;
    private static Map<Integer, Map<Integer, CrossGroupMammonListModel>> allCrossAreaGroupMammonListModelMap;
    private static Map<Long, Map<String, MammonRecord>> allCrossAreaMammonRecordMap;
    private static Map<Integer, Map<String, MammonRecord>> crossAreaGroupMammonRecordMap;
    private static Map<Integer, Map<Integer, CrossGroupMammonRecordListModel>> allCrossAreaGroupMammonRecordListModelMap;

    @Override
    public boolean init() throws Exception {
        addServerGroupReloadListener();
        addUserBaseInfoSyncListener();
        addServerGroup2ReloadListener();
        return reload();
    }

    private void addUserBaseInfoSyncListener() {
        CrossUserNotifyMgr.addSyncListener(new syncHandle());
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    @Override
    public boolean save() {
        allCrossRankListModelMap = new ConcurrentHashMap<>();
      /*  for(Map<Integer, Map<Integer, CrossRankListModel>> dataMap : allCrossRankListModelMap.values()) {
            for (Map<Integer, CrossRankListModel> m : dataMap.values()) {
                for (CrossRankListModel rankListModel : m.values()) {
                    rankListModel.saveRankList();
                }
            }
        }*/
        for(CrossUnionRankListModel rankListModel : allCrossUnionRankListModelMap.values()){
            rankListModel.saveRankList();
        }
        for(Map<Long, Map<Integer, CrossUserDecoration>> tempServerDecorationMap : allCrossUserMammonMap.values()){
            for(Map<Integer, CrossUserDecoration> userDecorationMap : tempServerDecorationMap.values()){
                for(CrossUserDecoration userDecoration : userDecorationMap.values()){
                    if(userDecoration.isInsertOption()){
                        CrossMammonBussiness.addCrossUserDecoration(userDecoration);
                    }else {
                        CrossMammonBussiness.updateCrossUserDecoration(userDecoration);
                    }
                }
            }
        }
        for (Map<Long, Map<Integer, CrossUserDecoration>> tempServerDecorationMap : allCrossAreaUserMammonMap.values()) {
            for (Map<Integer, CrossUserDecoration> userDecorationMap : tempServerDecorationMap.values()) {
                for (CrossUserDecoration userDecoration : userDecorationMap.values()) {
                    if (userDecoration.isInsertOption()) {
                        CrossAreaMammonBussiness.addCrossAreaUserDecoration(userDecoration);
                    } else {
                        CrossAreaMammonBussiness.updateCrossAreaUserDecoration(userDecoration);
                    }
                }
            }
        }
        for(Map<Integer, CrossGroupMammonListModel> tempMammonMap : allCrossGroupMammonListModelMap.values()){
            for(CrossGroupMammonListModel mammonListModel : tempMammonMap.values()){
                mammonListModel.checkOverTime();
            }
        }
        for (Map<Integer, CrossGroupMammonListModel> tempMammonMap : allCrossAreaGroupMammonListModelMap.values()) {
            for (CrossGroupMammonListModel mammonListModel : tempMammonMap.values()) {
                mammonListModel.checkOverTime();
            }
        }
        for(Map<String, MammonRecord> tempMammonRecordMap : allCrossMammonRecordMap.values()){
            for(MammonRecord mammonRecord : tempMammonRecordMap.values()){
                if(mammonRecord.isInsertOption()){
                    CrossMammonBussiness.addMammonRecord(mammonRecord);
                }else if(mammonRecord.isUpdateOption()){
                    CrossMammonBussiness.updateMammonRecord(mammonRecord);
                }
            }
        }
        for (Map<String, MammonRecord> tempMammonRecordMap : allCrossAreaMammonRecordMap.values()) {
            for (MammonRecord mammonRecord : tempMammonRecordMap.values()) {
                if (mammonRecord.isInsertOption()) {
                    CrossAreaMammonBussiness.addAreaMammonRecord(mammonRecord);
                } else if (mammonRecord.isUpdateOption()) {
                    CrossAreaMammonBussiness.updateAreaMammonRecord(mammonRecord);
                }
            }
        }
        return true;
    }

    @Override
    public boolean reloadData() {
        reloadRank();
        return true;
    }

    private void addServerGroupReloadListener(){
        ServerGroupMgr.addReloadListener(() -> {
            reloadGroupRank();
            notifyServerReload();
            logger.info("server group reload");
        });
    }

    private void addServerGroup2ReloadListener() {
        ServerGroup2Mgr.addReloadListener(() -> {
            reloadAreaMammon();
            notifyServerGroup2Reload();
            logger.info("server group 2 reload");
        });
    }

    private static void notifyServerReload(){
        int channelId = ConfigHelper.getInt("channelId");
        List<Long> serverList = ServerGroupMgr.getBeforeChangeServerList(channelId);
        List<ServerGroup> serverGroupList = ServerGroupMgr.getServerGroupList(channelId);
        for(ServerGroup serverGroup : serverGroupList){
            for(long serverId : serverGroup.getServerList()){
                if(!serverList.contains(serverId)){
                    serverList.add(serverId);
                }
            }
        }
        CommonProto.NullValueMsg.Builder msg = CommonProto.NullValueMsg.newBuilder();
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_CROSS_SERVER_GROUP_RELOAD_NOTIFY, msg);
        for(long serverId : serverList){
            MessageHelper.sendPacket(serverId, 0, pbMsg);
        }
    }

    private static void notifyServerGroup2Reload() {
        CommonProto.NullValueMsg.Builder msg = CommonProto.NullValueMsg.newBuilder();
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_CROSS_SERVER_GROUP_2_NOTIFY_RELOAD, msg);
        MessageHelper.sendAllServer(pbMsg);
    }

    public static void addUploadOverServer(long serverId) {
        synchronized (uploadOverServerIds) {
            if (uploadOverServerIds.contains(serverId)) {
                return;
            }
            uploadOverServerIds.add(serverId);
        }
    }

    public static boolean isUploadOver(long serverId) {
        synchronized (uploadOverServerIds) {
            return uploadOverServerIds.contains(serverId);
        }
    }

    private static void reloadRank(){
        allCrossRankListModelMap = new ConcurrentHashMap<>();
        allCrossUnionRankListModelMap = new ConcurrentHashMap<>();
        initCrossUnionRankListModelMap();

        // 跨服财神
        Map<Long, Map<Long, Map<Integer, CrossUserDecoration>>> tempCrossUserMammonMap = new ConcurrentHashMap<>();
        Map<Integer, List<CrossUserDecoration>> tempCrossMammonMap =  new ConcurrentHashMap<>();
        CrossMammonBussiness.getCrossUserMammonMap(tempCrossUserMammonMap, tempCrossMammonMap);
        allCrossUserMammonMap = tempCrossUserMammonMap;
        crossGroupMammonMap = tempCrossMammonMap;
        Map<Long, Map<String, MammonRecord>> tempCrossMammonRecordMap = new ConcurrentHashMap<>();
        Map<Integer, Map<String, MammonRecord>> tempCrossGroupMammonRecordMap = new ConcurrentHashMap<>();
        CrossMammonBussiness.getCrossMammonRecordMap(tempCrossMammonRecordMap, tempCrossGroupMammonRecordMap);
        allCrossMammonRecordMap = tempCrossMammonRecordMap;
        crossGroupMammonRecordMap = tempCrossGroupMammonRecordMap;

        // 跨服的跨地区财神
        Map<Long, Map<Long, Map<Integer, CrossUserDecoration>>> tempCrossAreaUserMammonMap = new ConcurrentHashMap<>();
        Map<Integer, List<CrossUserDecoration>> tempCrossAreaMammonMap = new ConcurrentHashMap<>();
        CrossAreaMammonBussiness.getCrossAreaUserMammonMap(tempCrossAreaUserMammonMap, tempCrossAreaMammonMap);
        allCrossAreaUserMammonMap = tempCrossAreaUserMammonMap;
        crossAreaGroupMammonMap = tempCrossAreaMammonMap;
        Map<Long, Map<String, MammonRecord>> tempCrossAreaMammonRecordMap = new ConcurrentHashMap<>();
        Map<Integer, Map<String, MammonRecord>> tempCrossAreaGroupMammonRecordMap = new ConcurrentHashMap<>();
        CrossAreaMammonBussiness.getCrossAreaMammonRecordMap(tempCrossAreaMammonRecordMap, tempCrossAreaGroupMammonRecordMap);
        allCrossAreaMammonRecordMap = tempCrossAreaMammonRecordMap;
        crossAreaGroupMammonRecordMap = tempCrossAreaGroupMammonRecordMap;

        //重新载入分组榜单
        reloadGroupRank();
        reloadAreaMammon();
    }

    private static void reloadGroupRank(){
        uploadOverServerIds = new ArrayList<>();
        allCrossGroupRankListModelMap = new ConcurrentHashMap<>();
        initCrossGroupRankListModelMap();
        allCrossGroupUnionRankListModelMap = new ConcurrentHashMap<>();
//        initCrossGroupUnionRankListModelMap();
        allCrossGroupMammonListModelMap = new ConcurrentHashMap<>();
        initCrossGroupMammonListModelMap();
        allCrossGroupMammonRecordListModelMap = new ConcurrentHashMap<>();
        initCrossGroupMammonRecordListModelMap();
    }

    private static void reloadAreaMammon() {
        allCrossAreaGroupMammonListModelMap = new ConcurrentHashMap<>();
        initCrossAreaGroupMammonListModelMap();
        allCrossAreaGroupMammonRecordListModelMap = new ConcurrentHashMap<>();
        initCrossAreaGroupMammonRecordListModelMap();
    }

    private static void initCrossGroupRankListModelMap(){
        List<ServerGroup> serverGroupList = ServerGroupMgr.getServerGroupList(ConfigHelper.getInt("channelId"));
        synchronized (allCrossGroupRankListModelMap) {
            for (ServerGroup serverGroup : serverGroupList) {
                if(!serverGroup.isCrossRank()){
                    continue;
                }
                Map<Integer, Map<Integer, CrossGroupRankListModel>> group = new ConcurrentHashMap<>();
                group.put(eBigRankType.Normal.getValue(), new ConcurrentHashMap<>());
                group.put(eBigRankType.Skin.getValue(), new ConcurrentHashMap<>());
                group.put(eBigRankType.Vehicle.getValue(), new ConcurrentHashMap<>());
                group.put(eBigRankType.Patrons.getValue(), new ConcurrentHashMap<>());
                group.put(eBigRankType.Beauty.getValue(), new ConcurrentHashMap<>());
                group.put(eBigRankType.CurioItem.getValue(), new ConcurrentHashMap<>());

                group.get(eBigRankType.Normal.getValue()).put(eRankType.EarnSpeed.getValue(), new CrossGroupRankListModel(serverGroup.getGroupId(), eRankType.EarnSpeed.getValue(), eBigRankType.Normal.getValue()));
                group.get(eBigRankType.Normal.getValue()).put(eRankType.PatronsAbility.getValue(), new CrossGroupRankListModel(serverGroup.getGroupId(), eRankType.PatronsAbility.getValue(), eBigRankType.Normal.getValue()));
                group.get(eBigRankType.Normal.getValue()).put(eRankType.BeautyIntimacy.getValue(), new CrossGroupRankListModel(serverGroup.getGroupId(), eRankType.BeautyIntimacy.getValue(), eBigRankType.Normal.getValue()));
                group.get(eBigRankType.Normal.getValue()).put(eRankType.EmbroideryMeter.getValue(), new CrossGroupRankListModel(serverGroup.getGroupId(), eRankType.EmbroideryMeter.getValue(), eBigRankType.Normal.getValue()));
                group.get(eBigRankType.Normal.getValue()).put(eRankType.ManorProsperousValue.getValue(), new CrossGroupRankListModel(serverGroup.getGroupId(), eRankType.ManorProsperousValue.getValue(), eBigRankType.Normal.getValue()));
                group.get(eBigRankType.Normal.getValue()).put(eRankType.WineShopPopularityValue.getValue(), new CrossGroupRankListModel(serverGroup.getGroupId(), eRankType.WineShopPopularityValue.getValue(), eBigRankType.Normal.getValue()));
                group.get(eBigRankType.Normal.getValue()).put(eRankType.FishPondScore.getValue(), new CrossGroupRankListModel(serverGroup.getGroupId(), eRankType.FishPondScore.getValue(), eBigRankType.Normal.getValue()));
                group.get(eBigRankType.Normal.getValue()).put(eRankType.GrandChildEarnSpeed.getValue(), new CrossGroupRankListModel(serverGroup.getGroupId(), eRankType.GrandChildEarnSpeed.getValue(), eBigRankType.Normal.getValue()));
                group.get(eBigRankType.Normal.getValue()).put(eRankType.AssistantDressOnValue.getValue(),new CrossGroupRankListModel(serverGroup.getGroupId(),eRankType.AssistantDressOnValue.getValue(),eBigRankType.Normal.getValue()));
                group.get(eBigRankType.Normal.getValue()).put(eRankType.WingRoomComfortValue.getValue(),new CrossGroupRankListModel(serverGroup.getGroupId(),eRankType.WingRoomComfortValue.getValue(),eBigRankType.Normal.getValue()));
                group.get(eBigRankType.Normal.getValue()).put(eRankType.DrugStroeTongBiValue.getValue(),new CrossGroupRankListModel(serverGroup.getGroupId(),eRankType.DrugStroeTongBiValue.getValue(),eBigRankType.Normal.getValue()));
                group.get(eBigRankType.Normal.getValue()).put(eRankType.WineryAroma.getValue(),new CrossGroupRankListModel(serverGroup.getGroupId(),eRankType.WineryAroma.getValue(),eBigRankType.Normal.getValue()));
                group.get(eBigRankType.Normal.getValue()).put(eRankType.CricketScore.getValue(),new CrossGroupRankListModel(serverGroup.getGroupId(),eRankType.CricketScore.getValue(),eBigRankType.Normal.getValue()));
                group.get(eBigRankType.Normal.getValue()).put(eRankType.KowloonProsperousValue.getValue(),new CrossGroupRankListModel(serverGroup.getGroupId(),eRankType.KowloonProsperousValue.getValue(),eBigRankType.Normal.getValue()));
                group.get(eBigRankType.Normal.getValue()).put(eRankType.WingRoomWindWaterValue.getValue(),new CrossGroupRankListModel(serverGroup.getGroupId(),eRankType.WingRoomWindWaterValue.getValue(),eBigRankType.Normal.getValue()));
                group.get(eBigRankType.Normal.getValue()).put(eRankType.MusicRoomRank.getValue(),new CrossGroupRankListModel(serverGroup.getGroupId(),eRankType.MusicRoomRank.getValue(),eBigRankType.Normal.getValue()));
                group.get(eBigRankType.Normal.getValue()).put(eRankType.CurioScoreValue.getValue(),new CrossGroupRankListModel(serverGroup.getGroupId(),eRankType.CurioScoreValue.getValue(),eBigRankType.Normal.getValue()));

                // TODO 增加榜单
                for (SkinInfo skinInfo : SkinMgr.getSkinInfoMap().values()) {
                    // 加载所有皮肤
                    group.get(eBigRankType.Skin.getValue()).put(skinInfo.getSkinId(), new CrossGroupRankListModel(serverGroup.getGroupId(), skinInfo.getSkinId(), eBigRankType.Skin.getValue()));
                }
                for (VehicleInfo vehicleInfo : VehicleMgr.getVehicleInfoMap().values()) {
                    // 加载部分珍兽
                    if (VehicleMgr.canJoinRank(vehicleInfo.getId())) {
                        group.get(eBigRankType.Vehicle.getValue()).put(vehicleInfo.getId(), new CrossGroupRankListModel(serverGroup.getGroupId(), vehicleInfo.getId(), eBigRankType.Vehicle.getValue()));
                    }
                }
                for (PatronsInfo patronsInfo : PatronsMgr.getPatronsInfoMap().values()) {
                    group.get(eBigRankType.Patrons.getValue()).put(patronsInfo.getId(), new CrossGroupRankListModel(serverGroup.getGroupId(), patronsInfo.getId(), eBigRankType.Patrons.getValue()));
                }
                group.get(eBigRankType.Patrons.getValue()).put(0, new CrossGroupRankListModel(serverGroup.getGroupId(), 0, eBigRankType.Patrons.getValue()));

                for (BeautyInfo beautyInfo : BeautyMgr.getBeautyInfoMap().values()) {
                    group.get(eBigRankType.Beauty.getValue()).put(beautyInfo.getBeautyId(), new CrossGroupRankListModel(serverGroup.getGroupId(), eRankType.Beauty.getValue(), eBigRankType.Beauty.getValue()));
                }
                group.get(eBigRankType.Beauty.getValue()).put(eRankType.Beauty.getValue(), new CrossGroupRankListModel(serverGroup.getGroupId(), eRankType.Beauty.getValue(), eBigRankType.Beauty.getValue()));

                for (CurioItemInfo itemInfo : CurioConfigMgr.getItemInfoMap().values()) {
                    group.get(eBigRankType.CurioItem.getValue()).put(itemInfo.getId(), new CrossGroupRankListModel(serverGroup.getGroupId(), eBigRankType.CurioItem.getValue(), itemInfo.getId()));
                }
                group.get(eBigRankType.CurioItem.getValue()).put(eRankType.Curio.getValue(), new CrossGroupRankListModel(serverGroup.getGroupId(), eBigRankType.CurioItem.getValue(), eRankType.Curio.getValue()));

                allCrossGroupRankListModelMap.put(serverGroup.getGroupId(), group);
            }

//            for (Map<Integer, Map<Integer, CrossGroupRankListModel>> groupMap : allCrossGroupRankListModelMap.values()) {
//                for (Map<Integer, CrossGroupRankListModel> dataMap : groupMap.values()) {
//                    for (CrossGroupRankListModel rankListModel : dataMap.values()) {
//                        rankListModel.setRankNum(GameConfig.RANK_DEFAULT_MAX_COUNT);
//                        LinkedList<CrossUserRank> dataList = new LinkedList<>();
//                        ServerGroup serverGroup = ServerGroupMgr.getServerGroup(rankListModel.getGroupId());
//                        if (null != serverGroup) {
//                            for (long serverId : serverGroup.getServerList()) {
//                                CrossRankListModel tempModel = getCrossRankListModel(serverId, rankListModel.getRankType(), rankListModel.getBigRankType());
//                                if (null != tempModel) {
//                                    LinkedList<CrossUserRank> tempRankList = tempModel.getRankList();
//                                    for(CrossUserRank tempRank : tempRankList){
//                                        dataList.add(tempRank.clone());
//                                    }
//                                }
//                            }
//                        }
//                        rankListModel.setRankList(dataList);
//                    }
//                }
//            }

        }
    }

    private static void initCrossUnionRankListModelMap(){
        Map<Long, LinkedList<UnionRank>> crossUnionRankListMap = RankBussiness.getCrossUnionRankListMap();
        for(Map.Entry<Long, LinkedList<UnionRank>> dataEntry : crossUnionRankListMap.entrySet()){
            CrossUnionRankListModel rankListModel = new CrossUnionRankListModel(dataEntry.getKey());
            rankListModel.setRankNum(GameConfig.RANK_DEFAULT_MAX_COUNT);
            rankListModel.setRankList(dataEntry.getValue());
            allCrossUnionRankListModelMap.put(dataEntry.getKey(), rankListModel);
        }
    }

    @Deprecated
    private static void initCrossGroupUnionRankListModelMap(){
        List<ServerGroup> serverGroupList = ServerGroupMgr.getServerGroupList(ConfigHelper.getInt("channelId"));
        synchronized (allCrossGroupUnionRankListModelMap) {
            for (ServerGroup serverGroup : serverGroupList) {
                if(!serverGroup.isCrossRank()){
                    continue;
                }
                CrossGroupUnionRankListModel rankListModel = new CrossGroupUnionRankListModel(serverGroup.getGroupId());
                rankListModel.setRankNum(GameConfig.RANK_DEFAULT_MAX_COUNT);
                LinkedList<UnionRank> dataList = new LinkedList<>();
                for (long serverId : serverGroup.getServerList()) {
                    CrossUnionRankListModel tempModel = getCrossUnionRankListModel(serverId);
                    if (null != tempModel) {
                        LinkedList<UnionRank> tempRankList = tempModel.getRankList();
                        for(UnionRank tempRank : tempRankList){
                            dataList.add(tempRank.clone());
                        }
                    }
                }
                rankListModel.setRankList(dataList);
                allCrossGroupUnionRankListModelMap.put(serverGroup.getGroupId(), rankListModel);
            }
        }
    }

    private static void initCrossGroupMammonListModelMap(){
        List<ServerGroup> serverGroupList = ServerGroupMgr.getServerGroupList(ConfigHelper.getInt("channelId"));
        synchronized (allCrossGroupMammonListModelMap) {
            for (ServerGroup serverGroup : serverGroupList) {
                if(0 == serverGroup.getCrossMammonState()){
                    continue;
                }
                Map<Integer, CrossGroupMammonListModel> modelMap = new ConcurrentHashMap<>();
                List<Integer> mammonTypeList = getMammonTypeList();
                for(int mammonType : mammonTypeList){
                    CrossGroupMammonListModel model = new CrossGroupMammonListModel(serverGroup.getGroupId(), mammonType);
                    model.setRankNum(Integer.MAX_VALUE);
                    LinkedList<CrossUserDecoration> dataList = new LinkedList<>();
                    for(CrossUserDecoration crossUserDecoration : getCrossUserDecorationList(mammonType)){
                        if(serverGroup.getServerList().contains(crossUserDecoration.getUserBaseInfo().getServerId())){
                            dataList.add(crossUserDecoration.clone());
                        }
                    }
                    model.setRankList(dataList);
                    modelMap.put(mammonType, model);
                }
                allCrossGroupMammonListModelMap.put(serverGroup.getGroupId(), modelMap);
            }
        }
    }

    private static void initCrossAreaGroupMammonListModelMap() {
        Map<Integer, ServerGroup2> groupMap = ServerGroup2Mgr.getGroupMap(eServerGroup2Type.CrossAreaMammon);
        synchronized (allCrossAreaGroupMammonListModelMap) {
            for (ServerGroup2 serverGroup2 : groupMap.values()) {
                Map<Integer, CrossGroupMammonListModel> modelMap = new ConcurrentHashMap<>();
                List<Integer> mammonTypeList = getAreaMammonTypeList();
                for (int mammonType : mammonTypeList) {
                    CrossGroupMammonListModel model = new CrossGroupMammonListModel(serverGroup2.getGroupId(), mammonType);
                    model.setRankNum(Integer.MAX_VALUE);
                    LinkedList<CrossUserDecoration> dataList = new LinkedList<>();
                    for (CrossUserDecoration crossUserDecoration : getCrossAreaUserDecorationList(mammonType)) {
                        if (serverGroup2.getServerList().contains(crossUserDecoration.getUserBaseInfo().getServerId())) {
                            dataList.add(crossUserDecoration.clone());
                        }
                    }
                    model.setRankList(dataList);
                    modelMap.put(mammonType, model);
                }
                allCrossAreaGroupMammonListModelMap.put(serverGroup2.getGroupId(), modelMap);
            }
        }
    }

    private static void initCrossGroupMammonRecordListModelMap(){
        List<ServerGroup> serverGroupList = ServerGroupMgr.getServerGroupList(ConfigHelper.getInt("channelId"));
        synchronized (allCrossGroupMammonRecordListModelMap) {
            for (ServerGroup serverGroup : serverGroupList) {
                if(0 == serverGroup.getCrossMammonState()){
                    continue;
                }
                Map<Integer, CrossGroupMammonRecordListModel> modelMap = new ConcurrentHashMap<>();
                List<Integer> mammonTypeList = getMammonRecordTypeList();
                for(int mammonType : mammonTypeList){
                    CrossGroupMammonRecordListModel model = new CrossGroupMammonRecordListModel(serverGroup.getGroupId(), mammonType);
                    Map<String, MammonRecord> dataMap = new ConcurrentHashMap<>();
                    for(MammonRecord mammonRecord : getMammonRecordMap(mammonType).values()){
                        if(serverGroup.getServerList().contains(mammonRecord.getUserBaseInfo().getServerId())){
                            dataMap.put(mammonRecord.getRecordUid(), mammonRecord);
                        }
                    }
                    model.setRecordMap(dataMap);
                    modelMap.put(mammonType, model);
                }
                allCrossGroupMammonRecordListModelMap.put(serverGroup.getGroupId(), modelMap);
            }
        }
    }

    private static void initCrossAreaGroupMammonRecordListModelMap() {
        Map<Integer, ServerGroup2> groupMap = ServerGroup2Mgr.getGroupMap(eServerGroup2Type.CrossAreaMammon);
        synchronized (allCrossAreaGroupMammonRecordListModelMap) {
            for (ServerGroup2 serverGroup2 : groupMap.values()) {
                Map<Integer, CrossGroupMammonRecordListModel> modelMap = new ConcurrentHashMap<>();
                List<Integer> mammonTypeList = getAreaMammonRecordTypeList();
                for (int mammonType : mammonTypeList) {
                    CrossGroupMammonRecordListModel model = new CrossGroupMammonRecordListModel(serverGroup2.getGroupId(), mammonType);
                    Map<String, MammonRecord> dataMap = new ConcurrentHashMap<>();
                    for (MammonRecord mammonRecord : getAreaMammonRecordMap(mammonType).values()) {
                        if (serverGroup2.getServerList().contains(mammonRecord.getUserBaseInfo().getServerId())) {
                            dataMap.put(mammonRecord.getRecordUid(), mammonRecord);
                        }
                    }
                    model.setRecordMap(dataMap);
                    modelMap.put(mammonType, model);
                }
                allCrossAreaGroupMammonRecordListModelMap.put(serverGroup2.getGroupId(), modelMap);
            }
        }
    }

    /**
     * 获取财神列表
     * @return
     */
    private static List<Integer> getMammonTypeList(){
        if(crossGroupMammonMap.size() > 0){
            return new ArrayList<>(crossGroupMammonMap.keySet());
        }
        return new ArrayList<>();
    }

    /**
     * 获取跨地区财神列表
     */
    private static List<Integer> getAreaMammonTypeList() {
        if (crossAreaGroupMammonMap.size() > 0) {
            return new ArrayList<>(crossAreaGroupMammonMap.keySet());
        }
        return new ArrayList<>();
    }

    /**
     * 获取财神集合
     * @param mammonType
     * @return
     */
    private static List<CrossUserDecoration> getCrossUserDecorationList(int mammonType){
        if(crossGroupMammonMap.containsKey(mammonType)){
            return new ArrayList<>(crossGroupMammonMap.get(mammonType));
        }
        return new ArrayList<>();
    }

    private static List<CrossUserDecoration> getCrossAreaUserDecorationList(int mammonType) {
        if (crossAreaGroupMammonMap.containsKey(mammonType)) {
            return new ArrayList<>(crossAreaGroupMammonMap.get(mammonType));
        }
        return new ArrayList<>();
    }

    /**
     * 获取财神簿类型列表
     * @return
     */
    private static List<Integer> getMammonRecordTypeList(){
        if(crossGroupMammonRecordMap.size() > 0){
            return new ArrayList<>(crossGroupMammonRecordMap.keySet());
        }
        return new ArrayList<>();
    }

    /**
     * 获取跨地区财神簿类型列表
     */
    private static List<Integer> getAreaMammonRecordTypeList() {
        if (crossAreaGroupMammonRecordMap.size() > 0) {
            return new ArrayList<>(crossAreaGroupMammonRecordMap.keySet());
        }
        return new ArrayList<>();
    }

    /**
     * 获取财神簿
     * @param mammonType
     * @return
     */
    private static Map<String, MammonRecord> getMammonRecordMap(int mammonType){
        if(crossGroupMammonRecordMap.containsKey(mammonType)){
            return new ConcurrentHashMap<>(crossGroupMammonRecordMap.get(mammonType));
        }
        return new ConcurrentHashMap<>();
    }

    /**
     * 获取跨地区财神簿
     */
    private static Map<String, MammonRecord> getAreaMammonRecordMap(int mammonType) {
        if (crossAreaGroupMammonRecordMap.containsKey(mammonType)) {
            return new ConcurrentHashMap<>(crossAreaGroupMammonRecordMap.get(mammonType));
        }
        return new ConcurrentHashMap<>();
    }

    private static CrossUnionRankListModel getCrossUnionRankListModel(long serverId){
        return allCrossUnionRankListModelMap.get(serverId);
    }

    private static Map<Integer, Map<Integer, CrossRankListModel>> getCrossRankListModelMap(long serverId){
        return allCrossRankListModelMap.get(serverId);
    }

    private static CrossRankListModel getCrossRankListModel(long serverId, int type, int bigRankType){
        Map<Integer, Map<Integer, CrossRankListModel>> modelMap  = getCrossRankListModelMap(serverId);
        if(null != modelMap && modelMap.containsKey(bigRankType)){
            return modelMap.get(bigRankType).get(type);
        }
        return null;
    }

    @Deprecated
    private static CrossGroupUnionRankListModel getCrossGroupUnionRankListModel(int groupId){
        return allCrossGroupUnionRankListModelMap.get(groupId);
    }

    private static Map<Integer, Map<Integer, CrossGroupRankListModel>> getCrossGroupRankListModelMap(int groupId){
        return allCrossGroupRankListModelMap.get(groupId);
    }

    private static CrossGroupRankListModel getCrossGroupRankListModel(int groupId, int type, int bigRankType){
        Map<Integer, Map<Integer, CrossGroupRankListModel>> modelMap = getCrossGroupRankListModelMap(groupId);
        if (modelMap != null && modelMap.containsKey(bigRankType)) {
            if (modelMap.get(bigRankType).get(type) != null) {
                return modelMap.get(bigRankType).get(type);
            }
        }
        return null;
    }

    private static Map<Integer, CrossGroupMammonListModel> getCrossGroupMammonListModelMap(int groupId){
        return allCrossGroupMammonListModelMap.get(groupId);
    }

    private static Map<Integer, CrossGroupMammonListModel> getCrossAreaGroupMammonListModelMap(int groupId) {
        return allCrossAreaGroupMammonListModelMap.get(groupId);
    }

    private static CrossGroupMammonListModel getCrossGroupMammonListModel(int groupId, int decorationId){
        Map<Integer, CrossGroupMammonListModel> modelMap = getCrossGroupMammonListModelMap(groupId);
        if(null != modelMap){
            return modelMap.get(decorationId);
        }
        return null;
    }

    private static CrossGroupMammonListModel getCrossAreaGroupMammonListModel(int groupId, int decorationId) {
        Map<Integer, CrossGroupMammonListModel> modelMap = getCrossAreaGroupMammonListModelMap(groupId);
        if (null != modelMap) {
            return modelMap.get(decorationId);
        }
        return null;
    }

    private static Map<Integer, CrossGroupMammonRecordListModel> getCrossGroupMammonRecordListModelMap(int groupId){
        return allCrossGroupMammonRecordListModelMap.get(groupId);
    }

    private static Map<Integer, CrossGroupMammonRecordListModel> getCrossAreaGroupMammonRecordListModelMap(int groupId) {
        return allCrossAreaGroupMammonRecordListModelMap.get(groupId);
    }

    private static CrossGroupMammonRecordListModel getCrossGroupMammonRecordListModel(int groupId, int decorationId){
        Map<Integer, CrossGroupMammonRecordListModel> modelMap = getCrossGroupMammonRecordListModelMap(groupId);
        if(null != modelMap){
            return modelMap.get(decorationId);
        }
        return null;
    }

    private static CrossGroupMammonRecordListModel getCrossAreaGroupMammonRecordListModel(int groupId, int decorationId) {
        Map<Integer, CrossGroupMammonRecordListModel> modelMap = getCrossAreaGroupMammonRecordListModelMap(groupId);
        if (null != modelMap) {
            return modelMap.get(decorationId);
        }
        return null;
    }

    public static void changeUserGroupRank(int groupId, int bigType, int type, CrossUserRank crossUserRank){
        CrossGroupRankListModel rankListModel = getCrossGroupRankListModel(groupId, type, bigType);
        if (null == rankListModel) {
            synchronized (allCrossGroupRankListModelMap) {
                if(!allCrossGroupRankListModelMap.containsKey(groupId)){
                    allCrossGroupRankListModelMap.put(groupId, new ConcurrentHashMap<>());
                }
                if (!allCrossGroupRankListModelMap.get(groupId).containsKey(bigType)) {
                    allCrossGroupRankListModelMap.get(groupId).put(bigType, new ConcurrentHashMap<>());
                }

                if (!allCrossGroupRankListModelMap.get(groupId).get(bigType).containsKey(type)) {
                    rankListModel = new CrossGroupRankListModel(groupId, type, bigType);
                    rankListModel.setRankNum(GameConfig.RANK_DEFAULT_MAX_COUNT);
                    allCrossGroupRankListModelMap.get(groupId).get(bigType).put(type, rankListModel);
                }
            }
            rankListModel = getCrossGroupRankListModel(groupId, type, bigType);
        }
        if (null != rankListModel) {
            if (bigType == eBigRankType.Patrons.getValue() && type == 0) {
                rankListModel.allPatronsRankChange(crossUserRank);

            } else if(bigType == eBigRankType.Beauty.getValue() && type == eRankType.Beauty.getValue()) {
                rankListModel.allBeautyRankChange(crossUserRank);
            } else if(bigType == eBigRankType.CurioItem.getValue()) {
                if(type == eRankType.Curio.getValue()){
                    rankListModel.allRankChange2(crossUserRank);
                }else {
                    rankListModel.rankChange2(crossUserRank);
                }
            } else {
                rankListModel.rankChange(crossUserRank);
            }
        }
     //   changeUserRank(type, crossUserRank, bigType);
    }

    @Deprecated
    public static void changeUnionGroupRank(int groupId, UnionRank unionRank){
        CrossGroupUnionRankListModel rankListModel = getCrossGroupUnionRankListModel(groupId);
        if (null == rankListModel) {
            synchronized (allCrossGroupUnionRankListModelMap) {
                if(!allCrossGroupUnionRankListModelMap.containsKey(groupId)){
                    rankListModel = new CrossGroupUnionRankListModel(groupId);
                    rankListModel.setRankNum(GameConfig.RANK_DEFAULT_MAX_COUNT);
                    allCrossGroupUnionRankListModelMap.put(groupId, rankListModel);
                }
            }
            rankListModel = getCrossGroupUnionRankListModel(groupId);
        }
        if (null != rankListModel) {
            if(unionRank.getUnionBaseInfo().getTotalEarnSpeed().compareTo(BigInteger.ZERO) == 0){
                rankListModel.removeRank(unionRank.getUnionUid());
            }else {
                rankListModel.rankChange(unionRank);
            }
        }
        changeUnionRank(unionRank);
    }

    private static void changeMammonGroupRank(int groupId, CrossUserDecoration crossUserDecoration){
        CrossGroupMammonListModel model = getCrossGroupMammonListModel(groupId, crossUserDecoration.getDecorationId());
        if (null == model) {
            synchronized (allCrossGroupMammonListModelMap) {
                if(!allCrossGroupMammonListModelMap.containsKey(groupId)){
                    allCrossGroupMammonListModelMap.put(groupId, new ConcurrentHashMap<>());
                }
                if (!allCrossGroupMammonListModelMap.get(groupId).containsKey(crossUserDecoration.getDecorationId())) {
                    model = new CrossGroupMammonListModel(groupId, crossUserDecoration.getDecorationId());
                    model.setRankNum(Integer.MAX_VALUE);
                    allCrossGroupMammonListModelMap.get(groupId).put(crossUserDecoration.getDecorationId(), model);
                }
            }
            model = getCrossGroupMammonListModel(groupId, crossUserDecoration.getDecorationId());
        }
        if (null != model) {
            model.rankChange(crossUserDecoration);
        }
    }

    private static void changeAreaMammonGroupRank(int groupId, CrossUserDecoration crossUserDecoration) {
        CrossGroupMammonListModel model = getCrossAreaGroupMammonListModel(groupId, crossUserDecoration.getDecorationId());
        if (null == model) {
            synchronized (allCrossAreaGroupMammonListModelMap) {
                if (!allCrossAreaGroupMammonListModelMap.containsKey(groupId)) {
                    allCrossAreaGroupMammonListModelMap.put(groupId, new ConcurrentHashMap<>());
                }
                if (!allCrossAreaGroupMammonListModelMap.get(groupId).containsKey(crossUserDecoration.getDecorationId())) {
                    model = new CrossGroupMammonListModel(groupId, crossUserDecoration.getDecorationId());
                    model.setRankNum(Integer.MAX_VALUE);
                    allCrossAreaGroupMammonListModelMap.get(groupId).put(crossUserDecoration.getDecorationId(), model);
                }
            }
            model = getCrossGroupMammonListModel(groupId, crossUserDecoration.getDecorationId());
        }
        if (null != model) {
            model.rankChange(crossUserDecoration);
        }
    }

    private static void changeUnionRank(UnionRank unionRank){
        long serverId = unionRank.getUnionBaseInfo().getServerId();
        if(0 == serverId){
            return;
        }
        CrossUnionRankListModel rankListModel = getCrossUnionRankListModel(serverId);
        if (null == rankListModel) {
            synchronized (allCrossUnionRankListModelMap) {
                if(!allCrossUnionRankListModelMap.containsKey(serverId)){
                    rankListModel = new CrossUnionRankListModel(serverId);
                    rankListModel.setRankNum(GameConfig.RANK_DEFAULT_MAX_COUNT);
                    allCrossUnionRankListModelMap.put(serverId, rankListModel);
                }
            }
            rankListModel = getCrossUnionRankListModel(serverId);
        }
        if (null != rankListModel) {
            rankListModel.rankChange(unionRank);
        }
    }

    public static void changeUserRank(int type, CrossUserRank crossUserRank, int bigType){
        long serverId = crossUserRank.getUserBaseInfo().getServerId();
        if(0 == serverId){
            return;
        }
        CrossRankListModel rankListModel = getCrossRankListModel(serverId, type, bigType);
        if (null == rankListModel) {
            synchronized (allCrossRankListModelMap) {
                if(!allCrossRankListModelMap.containsKey(serverId)){
                    allCrossRankListModelMap.put(serverId, new ConcurrentHashMap<>());
                }

                if(!allCrossRankListModelMap.get(serverId).containsKey(bigType)){
                    allCrossRankListModelMap.get(serverId).put(bigType, new ConcurrentHashMap<>());
                }

                if (!allCrossRankListModelMap.get(serverId).get(bigType).containsKey(type)) {
                    rankListModel = new CrossRankListModel(serverId, type, bigType);
                    rankListModel.setRankNum(GameConfig.RANK_DEFAULT_MAX_COUNT);
                    allCrossRankListModelMap.get(serverId).get(bigType).put(type, rankListModel);
                }
            }
            rankListModel = getCrossRankListModel(serverId, type, bigType);
        }
        if (null != rankListModel) {
            if (bigType == eBigRankType.Patrons.getValue() && type == 0) {
                rankListModel.allPatronsRankChange(crossUserRank);
            } else {
                rankListModel.rankChange(crossUserRank);
            }

        }
    }

    public static void changeUserMammon(int groupId, long userId, UserBaseInfo userBaseInfo, UserDecoration userDecoration){
        long serverId = userBaseInfo.getServerId();
        if(0 == serverId){
            return;
        }
        Map<Long, Map<Integer, CrossUserDecoration>> tempServerDecorationMap = allCrossUserMammonMap.get(serverId);
        if(null == tempServerDecorationMap){
            tempServerDecorationMap = new ConcurrentHashMap<>();
            allCrossUserMammonMap.put(serverId, tempServerDecorationMap);
        }
        Map<Integer, CrossUserDecoration> tempUserDecorationMap = tempServerDecorationMap.get(userId);
        if(null == tempUserDecorationMap){
            tempUserDecorationMap = new ConcurrentHashMap<>();
            tempServerDecorationMap.put(userId, tempUserDecorationMap);
        }
        CrossUserDecoration crossUserDecoration = tempUserDecorationMap.get(userDecoration.getDecorationId());
        if(null == crossUserDecoration){
            crossUserDecoration = new CrossUserDecoration();
            crossUserDecoration.setUserId(userId);
            crossUserDecoration.setDecorationId(userDecoration.getDecorationId());
            crossUserDecoration.setUserBaseInfo(userBaseInfo);
            crossUserDecoration.setEndTime(userDecoration.getEndTime());
            crossUserDecoration.setModifyTime(userDecoration.getModifyTime());
            crossUserDecoration.setInsertOption();
            tempUserDecorationMap.put(crossUserDecoration.getDecorationId(), crossUserDecoration);
            if(!crossGroupMammonMap.containsKey(crossUserDecoration.getDecorationId())){
                synchronized (crossGroupMammonMap){
                    if(!crossGroupMammonMap.containsKey(crossUserDecoration.getDecorationId())){
                        crossGroupMammonMap.put(crossUserDecoration.getDecorationId(), new ArrayList<>());
                    }
                }
            }
            List<CrossUserDecoration> dataList = crossGroupMammonMap.get(crossUserDecoration.getDecorationId());
            synchronized (dataList){
                Iterator<CrossUserDecoration> iterator = dataList.iterator();
                while (iterator.hasNext()) {
                    CrossUserDecoration data = iterator.next();
                    if(data.getUserId() == crossUserDecoration.getUserId() &&
                            data.getUserBaseInfo().getServerId() == crossUserDecoration.getUserBaseInfo().getServerId()){
                        iterator.remove();
                    }
                }
                dataList.add(crossUserDecoration);
            }
        }else {
            crossUserDecoration.setUserBaseInfo(userBaseInfo);
            crossUserDecoration.setEndTime(userDecoration.getEndTime());
            crossUserDecoration.setModifyTime(userDecoration.getModifyTime());
        }
        changeMammonGroupRank(groupId, crossUserDecoration);
    }

    public static void changeAreaUserMammon(int groupId, long userId, UserBaseInfo userBaseInfo, UserDecoration userDecoration) {
        long serverId = userBaseInfo.getServerId();
        if (0 == serverId) {
            return;
        }
        Map<Long, Map<Integer, CrossUserDecoration>> tempServerDecorationMap = allCrossAreaUserMammonMap.get(serverId);
        if (null == tempServerDecorationMap) {
            tempServerDecorationMap = new ConcurrentHashMap<>();
            allCrossAreaUserMammonMap.put(serverId, tempServerDecorationMap);
        }
        Map<Integer, CrossUserDecoration> tempUserDecorationMap = tempServerDecorationMap.get(userId);
        if (null == tempUserDecorationMap) {
            tempUserDecorationMap = new ConcurrentHashMap<>();
            tempServerDecorationMap.put(userId, tempUserDecorationMap);
        }
        CrossUserDecoration crossUserDecoration = tempUserDecorationMap.get(userDecoration.getDecorationId());
        if (null == crossUserDecoration) {
            crossUserDecoration = new CrossUserDecoration();
            crossUserDecoration.setUserId(userId);
            crossUserDecoration.setDecorationId(userDecoration.getDecorationId());
            crossUserDecoration.setUserBaseInfo(userBaseInfo);
            crossUserDecoration.setEndTime(userDecoration.getEndTime());
            crossUserDecoration.setModifyTime(userDecoration.getModifyTime());
            crossUserDecoration.setInsertOption();
            tempUserDecorationMap.put(crossUserDecoration.getDecorationId(), crossUserDecoration);
            if (!crossAreaGroupMammonMap.containsKey(crossUserDecoration.getDecorationId())) {
                synchronized (crossAreaGroupMammonMap) {
                    if (!crossAreaGroupMammonMap.containsKey(crossUserDecoration.getDecorationId())) {
                        crossAreaGroupMammonMap.put(crossUserDecoration.getDecorationId(), new ArrayList<>());
                    }
                }
            }
            List<CrossUserDecoration> dataList = crossAreaGroupMammonMap.get(crossUserDecoration.getDecorationId());
            synchronized (dataList) {
                Iterator<CrossUserDecoration> iterator = dataList.iterator();
                while (iterator.hasNext()) {
                    CrossUserDecoration data = iterator.next();
                    if (data.getUserId() == crossUserDecoration.getUserId() &&
                            data.getUserBaseInfo().getServerId() == crossUserDecoration.getUserBaseInfo().getServerId()) {
                        iterator.remove();
                    }
                }
                dataList.add(crossUserDecoration);
            }
        } else {
            crossUserDecoration.setUserBaseInfo(userBaseInfo);
            crossUserDecoration.setEndTime(userDecoration.getEndTime());
            crossUserDecoration.setModifyTime(userDecoration.getModifyTime());
        }
        changeAreaMammonGroupRank(groupId, crossUserDecoration);
    }

    public static void changeMammonRecord(int groupId, MammonRecord mammonRecord){
        long serverId = mammonRecord.getUserBaseInfo().getServerId();
        if(0 == serverId){
            return;
        }
        Map<String, MammonRecord> serverMap = allCrossMammonRecordMap.get(serverId);
        if(null == serverMap){
            synchronized (allCrossMammonRecordMap){
                if(!allCrossMammonRecordMap.containsKey(serverId)){
                    serverMap = new ConcurrentHashMap<>();
                    allCrossMammonRecordMap.put(serverId, serverMap);
                }
            }
            serverMap = allCrossMammonRecordMap.get(serverId);
        }
        if(!serverMap.containsKey(mammonRecord.getRecordUid())){
            mammonRecord.setInsertOption();
            serverMap.put(mammonRecord.getRecordUid(), mammonRecord);
            //加到集合
            if(!crossGroupMammonRecordMap.containsKey(mammonRecord.getType())){
                synchronized (crossGroupMammonRecordMap){
                    if(!crossGroupMammonRecordMap.containsKey(mammonRecord.getType())){
                        crossGroupMammonRecordMap.put(mammonRecord.getType(), new ConcurrentHashMap<>());
                    }
                }
            }
            Map<String, MammonRecord> dataMap = crossGroupMammonRecordMap.get(mammonRecord.getType());
            synchronized (dataMap){
                dataMap.put(mammonRecord.getRecordUid(), mammonRecord);
            }
        }

        changeGroupMammonRecord(groupId, mammonRecord);
    }

    public static void changeAreaMammonRecord(int groupId, MammonRecord mammonRecord) {
        long serverId = mammonRecord.getUserBaseInfo().getServerId();
        if (0 == serverId) {
            return;
        }
        Map<String, MammonRecord> serverMap = allCrossAreaMammonRecordMap.get(serverId);
        if (null == serverMap) {
            synchronized (allCrossAreaMammonRecordMap) {
                if (!allCrossAreaMammonRecordMap.containsKey(serverId)) {
                    serverMap = new ConcurrentHashMap<>();
                    allCrossAreaMammonRecordMap.put(serverId, serverMap);
                }
            }
            serverMap = allCrossAreaMammonRecordMap.get(serverId);
        }
        if (!serverMap.containsKey(mammonRecord.getRecordUid())) {
            mammonRecord.setInsertOption();
            serverMap.put(mammonRecord.getRecordUid(), mammonRecord);
            //加到集合
            if (!crossAreaGroupMammonRecordMap.containsKey(mammonRecord.getType())) {
                synchronized (crossAreaGroupMammonRecordMap) {
                    if (!crossAreaGroupMammonRecordMap.containsKey(mammonRecord.getType())) {
                        crossAreaGroupMammonRecordMap.put(mammonRecord.getType(), new ConcurrentHashMap<>());
                    }
                }
            }
            Map<String, MammonRecord> dataMap = crossAreaGroupMammonRecordMap.get(mammonRecord.getType());
            synchronized (dataMap) {
                dataMap.put(mammonRecord.getRecordUid(), mammonRecord);
            }
        }

        changeAreaGroupMammonRecord(groupId, mammonRecord);
    }

    private static void changeGroupMammonRecord(int groupId, MammonRecord mammonRecord){
        CrossGroupMammonRecordListModel model = getCrossGroupMammonRecordListModel(groupId, mammonRecord.getType());
        if (null == model) {
            synchronized (allCrossGroupMammonRecordListModelMap) {
                if(!allCrossGroupMammonRecordListModelMap.containsKey(groupId)){
                    allCrossGroupMammonRecordListModelMap.put(groupId, new ConcurrentHashMap<>());
                }
                if (!allCrossGroupMammonRecordListModelMap.get(groupId).containsKey(mammonRecord.getType())) {
                    model = new CrossGroupMammonRecordListModel(groupId, mammonRecord.getType());
                    allCrossGroupMammonRecordListModelMap.get(groupId).put(mammonRecord.getType(), model);
                }
            }
            model = getCrossGroupMammonRecordListModel(groupId, mammonRecord.getType());
        }
        if (null != model) {
            model.recordChange(mammonRecord);
        }
    }

    private static void changeAreaGroupMammonRecord(int groupId, MammonRecord mammonRecord) {
        CrossGroupMammonRecordListModel model = getCrossAreaGroupMammonRecordListModel(groupId, mammonRecord.getType());
        if (null == model) {
            synchronized (allCrossAreaGroupMammonRecordListModelMap) {
                if (!allCrossAreaGroupMammonRecordListModelMap.containsKey(groupId)) {
                    allCrossAreaGroupMammonRecordListModelMap.put(groupId, new ConcurrentHashMap<>());
                }
                if (!allCrossAreaGroupMammonRecordListModelMap.get(groupId).containsKey(mammonRecord.getType())) {
                    model = new CrossGroupMammonRecordListModel(groupId, mammonRecord.getType());
                    allCrossAreaGroupMammonRecordListModelMap.get(groupId).put(mammonRecord.getType(), model);
                }
            }
            model = getCrossAreaGroupMammonRecordListModel(groupId, mammonRecord.getType());
        }
        if (null != model) {
            model.recordChange(mammonRecord);
        }
    }

    public static Map<Integer, Map<Integer, Map<Integer, CrossGroupRankListModel>>> getAllCrossGroupRankListModelMap() {
        return new ConcurrentHashMap<>(allCrossGroupRankListModelMap);
    }

    @Deprecated
    public static Map<Integer, CrossGroupUnionRankListModel> getAllCrossGroupUnionRankListModelMap() {
        return new ConcurrentHashMap<>(allCrossGroupUnionRankListModelMap);
    }

    public static Map<Integer, Map<Integer, CrossGroupMammonListModel>> getAllCrossGroupMammonListModelMap() {
        return new ConcurrentHashMap<>(allCrossGroupMammonListModelMap);
    }

    public static Map<Integer, Map<Integer, CrossGroupMammonListModel>> getAllAreaCrossGroupMammonListModelMap() {
        return new ConcurrentHashMap<>(allCrossAreaGroupMammonListModelMap);
    }

    public static Map<Integer, Map<Integer, CrossGroupMammonRecordListModel>> getAllCrossGroupMammonRecordListModelMap() {
        return allCrossGroupMammonRecordListModelMap;
    }

    public static Map<Integer, Map<Integer, CrossGroupMammonRecordListModel>> getAllCrossAreaGroupMammonRecordListModelMap() {
        return allCrossAreaGroupMammonRecordListModelMap;
    }

    public class syncHandle implements IUserBaseInfoSyncListener {
        @Override
        public void notifySync(UserBaseInfo userBaseInfo, long userId) {
            notifyAllPatronsRankUserBaseInfoSync(userBaseInfo, userId);
        }
    }

    private void notifyAllPatronsRankUserBaseInfoSync(UserBaseInfo userBaseInfo, long userId) {
        int groupId = ServerGroupMgr.getGroupIdByServerId(userBaseInfo.getServerId());
        Map<Integer, Map<Integer, CrossGroupRankListModel>> groupRankMap = allCrossGroupRankListModelMap.get(groupId);
//        Map<Integer, Map<Integer, CrossRankListModel>> serverRankMap = allCrossRankListModelMap.get(userBaseInfo.getServerId());
        if (groupRankMap != null) {
            if (groupRankMap.containsKey(eBigRankType.Patrons.getValue())) {
                CrossGroupRankListModel crossGroupRankListModel = groupRankMap.get(eBigRankType.Patrons.getValue()).get(0);
                if (crossGroupRankListModel != null) {
                    for (CrossUserRank userRank : crossGroupRankListModel.getRankList()) {
                        if (userRank.getUserId() == userId) {
                            userRank.setUserBaseInfo(userBaseInfo);
                        }
                    }
                }
            }
        }

//        if (serverRankMap != null) {
//            if (serverRankMap.containsKey(eBigRankType.Patrons.getValue())) {
//                CrossRankListModel crossServerRankListModel = serverRankMap.get(eBigRankType.Patrons.getValue()).get(0);
//                if (crossServerRankListModel != null) {
//                    for (CrossUserRank userRank : crossServerRankListModel.getRankList()) {
//                        if (userRank.getUserId() == userId) {
//                            userRank.setUserBaseInfo(userBaseInfo);
//                            crossServerRankListModel.setUpdate(true);
//                        }
//                    }
//                }
//            }
//        }
    }

    /**
     * 跨服膜拜
     * @param req
     * @return
     */
    public static RankProto.CrossGroupWorshipRespMsg.Builder rankWorship(RankProto.CrossGroupWorshipReqMsg req){
        RankProto.CrossGroupWorshipRespMsg.Builder builder = RankProto.CrossGroupWorshipRespMsg.newBuilder();
        int groupId = req.getGroupId();
        int rankType = req.getCorssType();
        int gameType = req.getGameType();
        int bigType = req.getBigRankType();
        builder.setType(gameType);
        if (eRankType.CrossEarnSpeed.getValue() == gameType || eRankType.CrossPatronsAbility.getValue() == gameType ||
                eRankType.CrossBeautyIntimacy.getValue() == gameType) {
            Map<Integer, Map<Integer, Map<Integer, CrossGroupRankListModel>>> modelMap = CrossRankMgr.getAllCrossGroupRankListModelMap();
            Map<Integer, Map<Integer, CrossGroupRankListModel>> rankMap = modelMap.get(groupId);
            if (rankMap == null) {
                builder.setRet(GameErrorCode.E_RANK_WORSHIP_NO_DATA);
                return builder;
            }
            CrossGroupRankListModel rankListModel = rankMap.get(bigType).get(rankType);
            if (rankListModel == null) {
                builder.setRet(GameErrorCode.E_RANK_WORSHIP_NO_DATA);
                return builder;
            }
            RankProto.RankListMsg.Builder listMsg = rankListModel.getRankListMsg();
            if(null == listMsg || listMsg.getRankListCount() <= 0){
                builder.setRet(GameErrorCode.E_RANK_WORSHIP_NO_DATA);
                return builder;
            }
            RandomHelper randomHelper = new RandomHelper();
            int next = randomHelper.next(0, listMsg.getRankListCount());
            RankProto.RankTempMsg.Builder userRankMsg = listMsg.getRankListBuilder(next);
            int rankNum = next + 1;
            if(userRankMsg != null){
                builder.setRankData(userRankMsg);
            }
            builder.setRankNum(rankNum);
        } else if (eRankType.CrossUnionEarnSpeed.getValue() == rankType) {
            CrossGroupUnionRankListModel rankListModel = CrossRankMgr.getAllCrossGroupUnionRankListModelMap().get(groupId);
            if (rankListModel == null) {
                builder.setRet(GameErrorCode.E_RANK_WORSHIP_NO_DATA);
                return builder;
            }
            RankProto.UnionRankListMsg.Builder listMsg = rankListModel.getRankListMsg();
            if(null == listMsg || listMsg.getRankListCount() <= 0){
                builder.setRet(GameErrorCode.E_RANK_WORSHIP_NO_DATA);
                return builder;
            }
            RandomHelper randomHelper = new RandomHelper();
            int next = randomHelper.next(0, listMsg.getRankListCount());
            RankProto.UnionRankTempMsg.Builder unionRankMsg = listMsg.getRankListBuilder(next);
            if(unionRankMsg != null){
                builder.setUnionRankData(unionRankMsg);
            }
            int rankNum = next + 1;
            builder.setRankNum(rankNum);
        }
        builder.setRet(0);
        return builder;
    }


}
